package com.sc.datacollect.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;

public class SiteUtil {

	private static Map<String, String> sites = new HashMap<String, String>();
	
	public static String getSiteName(String siteCode, String monitorTime) throws Exception {
		if (sites.containsKey(siteCode)) {
			return sites.get(siteCode);
		} else {
			Connection conn = DBConnPool.getConnection();
			PreparedStatement psmt = null;
			ResultSet rs = null;
			String sql = " select t.sitecode, s.sitename || '-' || t.rangename sitename" + 
					" from M_SITE_RANGE t left join m_site s on s.sitecode = t.sitecode" + 
					" where" + 
					" t.sitecode = '" + siteCode + "'" + 
					" and t.start_date <= to_date('" + monitorTime + "', 'yyyy-mm-dd hh24:mi:ss')" + 
					" and t.end_date >= to_date('" + monitorTime + "', 'yyyy-mm-dd hh24:mi:ss')" + 
					" and t.is_use = 1" + 
					" and s.is_use = 1";
			
			conn = DBConnPool.getConnection();
			psmt = conn.prepareStatement(sql);
			rs = psmt.executeQuery();
			while (rs.next()) {
				sites.put(rs.getString(1), rs.getString(2));
			}
			DBConnPool.free(conn, psmt, rs);
			
			return sites.get(siteCode);
		}
	}
}